package com.tsj.spzx.order.controller;

import com.github.pagehelper.PageInfo;
import com.tsj.spzx.model.dto.h5.OrderInfoDto;
import com.tsj.spzx.model.entity.order.OrderInfo;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.model.vo.h5.TradeVo;
import com.tsj.spzx.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order/orderInfo")
@Tag(name = "订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    /**
     * 流程解释：
     *       将商品加入购物车后，在购物车页面点击去结算（对应的是 **确认下单** 返回的trade信息用于创建订单页面数据展示），跳转到创建订单页面，
     *       在创建订单页面点击 **提交订单** ，会将订单数据分别存入数据库的表中 order_info表和order_item表
     *       ，以及记录日志表 order_log表，然后跳转到支付页面, 并立即 **根据订单id查询订单信息**，在支付页面显示金额
     *
     *       当点击 **立即购买**后（同样需要返回trade信息）用于跳转到创建订单页面时的数据展示, 后续 **提交订单**
     *
     */

    /**
     * openFeign在远程调用的时候会丢失请求头，我们使用openFeign的拦截器传递请求头
     * 确认下单：
     * @return
     */
    @Operation(summary = "确认下单")
    @GetMapping("/auth/trade")
    public Result<TradeVo> trade() {
        TradeVo tradeVo = orderService.trade();
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 提交订单，
     * @return 返回订单id
     */
    @Operation(summary = "提交订单")
    @PostMapping("/auth/submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfoDto orderInfoDto) {
        Long orderId = orderService.submitOrder(orderInfoDto);
        return Result.build(orderId, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据订单id查询订单信息")
    @GetMapping("/auth/{orderId}")
    public Result getOrderInfoById(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderService.getOrderInfoById(orderId);
        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "立即购买")
    @GetMapping("/auth/buy/{skuId}")
    public Result buy(@PathVariable("skuId") Long skuId){
        TradeVo tradeVo = orderService.buy(skuId);
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据订单状态获取用户订单
     * @param page
     * @param limit
     * @param orderStatus 订单状态 不是必须
     * @return
     */
    @Operation(summary = "获取订单分页列表")
    @GetMapping("auth/{page}/{limit}")
    public Result getOrderPageList(@PathVariable("page") Integer page,
                                   @PathVariable("limit") Integer limit,
                                   @RequestParam(required = false, defaultValue = "") Integer orderStatus) {
        PageInfo<OrderInfo> pageInfo = orderService.getOrderPageList(page, limit, orderStatus);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 远程调用
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单编号查询订单详细信息")
    @GetMapping("/auth/getOrderInfo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(@PathVariable("orderNo") String orderNo) {
        OrderInfo orderInfo = orderService.getOrderInfoByOrderNo(orderNo);
        return orderInfo;
    }

    /**
     * 远程调用
     * @param orderNo
     * @return
     */
    @Operation(summary = "当支付完成后，修改订单接口")
    @GetMapping("/auth/updateOrderInfoStatusPayed/{orderNo}")
    public Result updateOrderInfoStatusPayed(@PathVariable("orderNo") String orderNo) {
        orderService.updateOrderInfoStatusPayed(orderNo);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
